
#ifndef SWITCHBUTTON_H
#define SWITCHBUTTON_H

#include <QWidget>
#include <QTimer>
#include <QPainter>

class SwitchButton : public QWidget {
    Q_OBJECT
public:
    /**
    * @brief 初始化构造函数
    */
    explicit SwitchButton(QWidget *parent = nullptr);

    /**
    * @brief 返回滑动按键的bool值
    */
    bool isChecked() const;

    /**
    * @brief 设置滑动按键的bool值,开启绘制按键图标
    * @param  checked 滑动控件对应的bool值
    */
    void setChecked(bool checked);

    /**
    * @brief 设置按键大小
    * @param  width 宽度
    * @param  height 高度
    */
    void setSize(int width, int height);

signals:
    void toggled(bool checked);

protected:
    void paintEvent(QPaintEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;

private slots:
    void updateAnimation();

private:
    bool m_checked;
    QTimer m_timer;
    qreal m_sliderPos;
    QColor m_bgColorOff = QColor(199, 199, 199);
    QColor m_bgColorOn = Qt::green;
    QColor m_sliderColor = Qt::white;
    qreal m_radius;
};
#endif // SWITCHBUTTON_H
